package com.zhengx.fragmentutildemo.pager;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.zhengx.fragmentutil.BaseQuickFragment;
import com.zhengx.fragmentutil.FragController;
import com.zhengx.fragmentutil.FragmentUtil;
import com.zhengx.fragmentutildemo.R;
import com.zhengx.fragmentutildemo.pager.item.FragmentItem;

/**
 * name：com.zhengx.fragmentutildemo
 * class: describe
 * author: zhengx
 * create_time: 2019/5/7
 */
public class FragmentPagerTwo extends BaseQuickFragment {

    private FragController controller;

    private Fragment[] fragments;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public void onResume() {
        super.onResume();
        Log.e(FragmentPagerTwo.class.getSimpleName(), " frag 可见");
    }

    @Override
    public void onPause() {
        super.onPause();
        Log.e(FragmentPagerTwo.class.getSimpleName(), " frag 不可见");
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.layout_fragment_pager_two, container, false);
        controller = FragmentUtil.findController(this);
        return view;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        view.findViewById(R.id.btn_tab_1).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showTab(1);
            }
        });
        view.findViewById(R.id.btn_tab_2).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showTab(2);
            }
        });
        view.findViewById(R.id.btn_tab_3).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showTab(3);
            }
        });
        view.findViewById(R.id.btn_tab_4).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showTab(4);
            }
        });
    }

    private void showTab(int NO) {
        for (int i = 0; i < fragments.length; i++) {
            if (NO == i + 1) {
                controller.show(FragmentItem.class, "" + (i + 1));
            } else {
                controller.hide(FragmentItem.class, "" + (i + 1));
            }
        }
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        String[] tags = new String[]{"1", "2", "3", "4"};
        FragmentItem[] fragmentItems = new FragmentItem[4];
        if ((fragmentItems[0] = (FragmentItem) controller.findFragment(FragmentItem.class, "1")) == null) {
            fragmentItems[0] = FragmentItem.create(1);
        }
        if ((fragmentItems[1] = (FragmentItem) controller.findFragment(FragmentItem.class, "2")) == null) {
            fragmentItems[1] = FragmentItem.create(2);
        }
        if ((fragmentItems[2] = (FragmentItem) controller.findFragment(FragmentItem.class, "3")) == null) {
            fragmentItems[2] = FragmentItem.create(3);
        }
        if ((fragmentItems[3] = (FragmentItem) controller.findFragment(FragmentItem.class, "4")) == null) {
            fragmentItems[3] = FragmentItem.create(4);
        }
        fragments = controller.initMultiFragments(fragmentItems, tags);

    }
}
